package org.odst.parser;

import java.util.ArrayList;
import java.util.List;


import org.odftoolkit.odfdom.doc.table.OdfTableCell;
import org.odftoolkit.odfdom.type.CellRangeAddress;
import org.odst.formula.Formula;
import org.odst.tag.Tag;
import org.odst.transformer.Row;
import org.odst.transformer.RowCollection;

/**
 * Represents excel cell
 *
 * @author Leonid Vysochyn
 * @author Cyril PODER
 */
public class Cell {

    private Row row;
    private Property collectionProperty;
    private OdfTableCell oooCell;

    private Formula formula;
    private String label;
    private int dependentRowNumber;

    private String collectionName;

    private String oooCellValue;
    private String stringCellValue;
    private String metaInfo;

    private CellRangeAddress mergedRegion;

    private List<Expression> expressions = new ArrayList<Expression>();

    private Tag tag;

    private RowCollection rowCollection;


    public Cell(OdfTableCell hssfCell, Row row) {
        this.setOooCell(hssfCell);
        this.setRow(row);
    }


    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public List<Expression> getExpressions() {
        return expressions;
    }

    public void setExpressions(List<Expression> expressions) {
        this.expressions = expressions;
    }

    public CellRangeAddress getMergedRegion() {
        return mergedRegion;
    }

    public int getDependentRowNumber() {
        return dependentRowNumber;
    }


    public RowCollection getRowCollection() {
        return rowCollection;
    }

    public void setRowCollection(RowCollection rowCollection) {
        this.rowCollection = rowCollection;
    }

    public String getCollectionName() {
        return collectionName;
    }

    public Formula getFormula() {
        return formula;
    }

    public void setFormula(Formula formula) {
        this.formula = formula;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Property getCollectionProperty() {
        return collectionProperty;
    }

    public void setCollectionProperty(Property collectionProperty) {
        this.collectionProperty = collectionProperty;
    }

    public OdfTableCell getOooCell() {
        return oooCell;
    }

    public void setOooCell(OdfTableCell hssfCell) {
        this.oooCell = hssfCell;
    }

    public void replaceCellWithNewShiftedBy(int shift){
        OdfTableCell newHSSFCell = row.getOooRow().getCellByIndex(oooCell.getColumnIndex() + shift);
        this.oooCell = newHSSFCell;
    }

    public String toCellName() {
        CellReference cellRef = new CellReference(getOooCell());
        return cellRef.formatAsString();
    }

    public Row getRow() {
        return row;
    }

    public void setRow(Row row) {
        this.row = row;
    }

    public boolean isFormula() {
        return getFormula() != null;
    }


    public String getOooCellValue() {
        return oooCellValue;
    }

    public String getStringCellValue() {
        return stringCellValue;
    }

    public String getMetaInfo() {
        return metaInfo;
    }

    public boolean isEmpty() {
        return getOooCellValue() == null || getOooCellValue().length() == 0 || getOooCell().getValueType() == null || getOooCell().getValueType().equals("null");
    }

    public boolean isNull() {
        return getOooCell() == null;
    }

    public String toString() {
        return getOooCellValue();
    }

    public void setDependentRowNumber(int dependentRowNumber) {
        this.dependentRowNumber = dependentRowNumber;
    }

    public void setCollectionName(String collectionName) {
        this.collectionName = collectionName;
    }

    public void setOooCellValue(String hssfCellValue) {
        this.oooCellValue = hssfCellValue;
    }

    public void setStringCellValue(String stringCellValue) {
        this.stringCellValue = stringCellValue;
    }

    public void setMetaInfo(String metaInfo) {
        this.metaInfo = metaInfo;
    }

    public void setMergedRegion(CellRangeAddress mergedRegion) {
        this.mergedRegion = mergedRegion;
    }
}
